package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.CommonReviewInfo;
import com.ruoyi.business.domain.MessageCenter;
import com.ruoyi.business.domain.vo.CommonReviewVO;
import com.ruoyi.business.service.ICommonReviewInfoService;
import com.ruoyi.business.service.IMessageCenterService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.RepairEventsMapper;
import com.ruoyi.business.domain.RepairEvents;
import com.ruoyi.business.service.IRepairEventsService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.business.weapp.CommonContants.REVIEW_TYPE_REPAIR;

/**
 * 报事报修Service业务层处理
 * 
 * @author asher
 * @date 2021-11-23
 */
@Service
public class RepairEventsServiceImpl implements IRepairEventsService 
{
    @Autowired
    private RepairEventsMapper repairEventsMapper;

    @Autowired
    private ICommonReviewInfoService commonReviewInfoService;

    @Autowired
    private IMessageCenterService messageCenterService;

    /**
     * 查询报事报修
     * 
     * @param eventId 报事报修主键
     * @return 报事报修
     */
    @Override
    public RepairEvents selectRepairEventsByEventId(Long eventId)
    {
        return repairEventsMapper.selectRepairEventsByEventId(eventId);
    }

    /**
     * 查询报事报修列表
     * 
     * @param repairEvents 报事报修
     * @return 报事报修
     */
    @Override
    public List<RepairEvents> selectRepairEventsList(RepairEvents repairEvents)
    {
        return repairEventsMapper.selectRepairEventsList(repairEvents);
    }

    /**
     * 新增报事报修
     * 
     * @param repairEvents 报事报修
     * @return 结果
     */
    @Override
    public int insertRepairEvents(RepairEvents repairEvents)
    {
        repairEvents.setCreateTime(DateUtils.getNowDate());
        return repairEventsMapper.insertRepairEvents(repairEvents);
    }

    /**
     * 修改报事报修
     * 
     * @param repairEvents 报事报修
     * @return 结果
     */
    @Override
    public int updateRepairEvents(RepairEvents repairEvents)
    {
        repairEvents.setUpdateTime(DateUtils.getNowDate());
        return repairEventsMapper.updateRepairEvents(repairEvents);
    }

    /**
     * 批量删除报事报修
     * 
     * @param eventIds 需要删除的报事报修主键
     * @return 结果
     */
    @Override
    public int deleteRepairEventsByEventIds(Long[] eventIds)
    {
        return repairEventsMapper.deleteRepairEventsByEventIds(eventIds);
    }

    /**
     * 删除报事报修信息
     * 
     * @param eventId 报事报修主键
     * @return 结果
     */
    @Override
    public int deleteRepairEventsByEventId(Long eventId)
    {
        return repairEventsMapper.deleteRepairEventsByEventId(eventId);
    }

    @Override
    @Transactional
    public int handleRepairEvents(CommonReviewVO commonReviewVO) {
        RepairEvents repairEvents = repairEventsMapper.selectRepairEventsByEventId(commonReviewVO.getRecordId());
        if (repairEvents == null) {
            return 0;
        }

        CommonReviewInfo commonReviewInfo = new CommonReviewInfo();
        commonReviewInfo.setOriginalStatus(repairEvents.getStatus());
        commonReviewInfo.setReviewContent(commonReviewVO.getContent());
        commonReviewInfo.setDestStatus(commonReviewVO.getStatus());
        commonReviewInfo.setCreateBy(commonReviewVO.getUserId().toString());
        commonReviewInfo.setBusinessId(commonReviewVO.getRecordId());
        commonReviewInfo.setReviewType(REVIEW_TYPE_REPAIR);
        repairEvents.setStatus(commonReviewVO.getStatus());

        MessageCenter messageCenter = new MessageCenter();
        messageCenter.setTitle("报事报修进度提醒");
        String content = "您上报的问题正在处理中，感谢您的反馈";
        if (commonReviewVO.getStatus().equals("1")) {
            content = "您上报的问题正在处理中，来自物业的回复：" + commonReviewVO.getContent();
        }
        if (commonReviewVO.getStatus().equals("2")) {
            content = "您上报的问题已经处理完成，感谢您的反馈。来自物业的回复：" + commonReviewVO.getContent();;
        }
        messageCenter.setContent("[报事报修]" + content);
        messageCenter.setIsRead("0");
        messageCenter.setModule("报事报修");
        messageCenter.setDestUserId(repairEvents.getUserId().toString());
        messageCenterService.insertMessageCenter(messageCenter);

        return commonReviewInfoService.insertCommonReviewInfo(commonReviewInfo) & repairEventsMapper.updateRepairEvents(repairEvents);
    }
}
